//
//  NSString+EL.m
//  JTProject
//
//  Created by Yuki on 2021/3/9.
//

#import "NSString+EL.h"
#import <objc/runtime.h>
#import <CommonCrypto/CommonCrypto.h>

@implementation NSString (EL)

#pragma mark - --------- MD5  ---------
///小写
- (NSString *)md5 {
    if(self.length == 0) {
        return nil;
    }
    
    const char *cStr = [self UTF8String];
    unsigned char result[16];
    CC_MD5(cStr, (CC_LONG)strlen(cStr), result);
    
    return [[NSString stringWithFormat:@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
             result[0], result[1], result[2], result[3], result[4], result[5], result[6], result[7],
             result[8], result[9], result[10], result[11],result[12], result[13], result[14], result[15]] lowercaseStringWithLocale:[NSLocale currentLocale]] ;
}
///大写
- (NSString *)MD5 {
    if(self.length == 0) {
        return nil;
    }
    
    const char *cStr = [self UTF8String];
    unsigned char result[16];
    CC_MD5(cStr, (CC_LONG)strlen(cStr), result);
    
    return [[NSString stringWithFormat:@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
             result[0], result[1], result[2], result[3], result[4], result[5], result[6], result[7],
             result[8], result[9], result[10], result[11],result[12], result[13], result[14], result[15]] uppercaseStringWithLocale:[NSLocale currentLocale]] ;
}


#pragma mark - --------- 获取文本的宽度高度  ---------
///获取文本的最大高度
+ (CGFloat)getStrMaxHeightWithStr:(NSString *)str
                    numberofLines:(NSInteger)numberofLines
                             font:(UIFont *)font
                         maxWidth:(CGFloat)maxWidth{
    UILabel *lb = [UILabel new];
    lb.text = str;
    lb.numberOfLines = numberofLines;
    lb.font = font;
    CGSize retSize = [lb sizeThatFits:CGSizeMake(maxWidth, CGFLOAT_MAX)];
    return ceilf(retSize.height);
}
///获取文本的最大宽度
+ (CGFloat)getStrMaxWidthWithStr:(NSString *)str
                            font:(UIFont *)font
{
    UILabel *lb = [UILabel new];
    lb.text = str;
    lb.font = font;
    lb.numberOfLines = 1;
    CGSize retSize = [lb sizeThatFits:CGSizeMake(CGFLOAT_MAX, 20)];
    return ceilf(retSize.width);
}

#pragma mark - --------- 获取富文本的最大高度  ---------
+ (CGFloat)getAttriStrMaxHeightWithAttri:(NSMutableAttributedString *)attriStr
                                           MaxWidth:(CGFloat)maxWidth{
    UILabel *lb = [UILabel new];
    lb.attributedText = attriStr;
    lb.numberOfLines = 0;
    CGSize retSize = [lb sizeThatFits:CGSizeMake(maxWidth, CGFLOAT_MAX)];
    return ceilf(retSize.height);
}
#pragma mark - --------- 判断字符串是否为空  ---------
- (BOOL)isEmptyStr{
    BOOL isEmpty = NO;
    /// 为nil，为@"", 长度
    if (self == nil || //为nil
        [self isEqualToString:@"<null>"] ||
        [self isEqualToString:@"(null)"] ||
        [self isEqualToString:@"null"] ||
        [self isEqualToString:@""] || //为@”“
        self.length == 0 || //长度
        [self stringByReplacingOccurrencesOfString:@" " withString:@""].length == 0 //替换了空格后的长度
        )
    {
        isEmpty = YES;
        return isEmpty;
    }else{
        isEmpty = NO;
        return isEmpty;
    }
}

#pragma mark - --------- 是否是手机号  ---------
- (BOOL)isMobilePhone{
    if (self.length != 11) return NO;///长度如果不满足
    //手机号以13， 15，18开头，八个 \d 数字字符
    NSString *phoneRegex = @"^((11[0-9])|(12[0-9])|(13[0-9])|(14[0-9])|(15[^4,\\D])|(16[0-9])|(17[0-9])|(18[0,0-9])|(19[0,0-9]))\\d{8}$";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    return [phoneTest evaluateWithObject:self];
}
@end
